package com.googlecode.wstester.config

import java.io.File
import utils._
import xml.{Elem, XML}
import collection.mutable.ArrayBuffer

/**
 * Created by IntelliJ IDEA.
 * User: pablo
 * Date: 2010-07-05
 * Time: 09:11:47
 */

class TestConfig(val testDir: File) {
  val confFile:File = new File(testDir.getName+"/test.xml");
  var config: Elem = null;
  var testType: TestType = null;
  var requests: List[RequestConfig] = Nil;
  var tmp = new ArrayBuffer[RequestConfig];
  def parseConfig = {
    if(confFile exists) {
      config = XML.loadFile(confFile);
      config\\"test" foreach {(test) =>
        (test\\"test" \ "@type").text match {
          case "simple" => testType = new Simple;
          case "conversation" => testType = new Conversation;
          case "oneshot" => testType = new OneShot;
          case _ => throw new IllegalArgumentException("Unknown test type");
        }
      }
      config\\"request" foreach {(request) =>
        var requestConfig = new RequestConfig;
        (request\ "@type").text match {
          case "plainXML" => requestConfig.requestType = new PlainXML;
          case "velocity" => requestConfig.requestType = new Velocity;
          case _ => throw new IllegalArgumentException("Unknown request type");
        }
        requestConfig.requestFileName = request\\"requestMessageFile" text;
        requestConfig.targetUrl = request\\"targetURL" text;
        requestConfig.soapAction = request\\"soapAction" text;
        tmp += requestConfig;
        requests = tmp.toList;        
      }
    }
  }
}